<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .box {
        width: 800px;
        margin: 0 auto;
      }
      .lazy-load {
        width: 800px;
        height: 300px;
        display: block;
        background-color: #eee;
        /* 取消轮廓线 */
        outline: none;
        border: 0;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <img class="lazy-load" data-src="./images/001.jpg" />
      <img class="lazy-load" data-src="./images/002.jpg" />
      <img class="lazy-load" data-src="./images/003.jpg" />
      <img class="lazy-load" data-src="./images/004.jpg" />
    </div>
  </body>
  <!-- 引入懒加载JS文件 -->
  <script src="./lazyLoad(懒加载).js"></script>
  <script>
    var imgs = document.getElementsByClassName("lazy-load");
    //在屏幕滚动前先调用一次懒加载把可以加载的图片加载完成
    lazyLoad(imgs);
    // 当屏幕滚动时调用懒加载
    window.onscroll = function () {
      lazyLoad(imgs);
    };
  </script>
</html>
